<?php
session_start();
$HBS = [
    "HT-200"=>['min'=>170,'max'=>241, 'a'=>0],
    "HT-300"=>['min'=>187,'max'=>255, 'a'=>1],
    "HT-350"=>['min'=>197,'max'=>169, 'a'=>2],
    "QT500-7-正火"=>['min'=>170,'max'=>230, 'a'=>3],
    "QT600-3-正火"=>['min'=>190,'max'=>270, 'a'=>4],
    "ZG310-570-正火"=>['min'=>163,'max'=>197, 'a'=>5],
    "ZG340-640-正火"=>['min'=>169,'max'=>207, 'a'=>6],
    "45-正火"=>['min'=>162,'max'=>217, 'a'=>7],
    "45-调质"=>['min'=>197,'max'=>269, 'a'=>8],
    "ZG35SiMn-正火-h"=>['min'=>163,'max'=>217, 'a'=>9],
    "ZG35SiMn-调质"=>['min'=>197,'max'=>248, 'a'=>10],
    "ZG35CrMnSi-正火-h"=>['min'=>162,'max'=>217, 'a'=>11],
    "ZG35CrMnSi-调质"=>['min'=>162,'max'=>217, 'a'=>11.5],
    "35CrMo-调质"=>['min'=>241,'max'=>286, 'a'=>12],
    "40MnB-调质"=>['min'=>241,'max'=>286, 'a'=>13],
    "37SiMn2MoV-调质"=>['min'=>241,'max'=>302, 'a'=>14],
    "40Cr-调质"=>['min'=>241,'max'=>302, 'a'=>8],
    "ZG35SiMn-正火或调质-表面淬火"=>['min'=>450,'max'=>530, 'a'=>15],
    "45-正火或调质-表面淬火"=>['min'=>400,'max'=>500, 'a'=>16],
    "35CrMo-调质-表面淬火"=>['min'=>450,'max'=>550, 'a'=>17],
    "40MnB-调质-表面淬火"=>['min'=>450,'max'=>550, 'a'=>18],
    "37SiMn2MoV-调质-表面淬火"=>['min'=>500,'max'=>550, 'a'=>19],
    "40Cr-调质-表面淬火"=>['min'=>480,'max'=>550, 'a'=>20],
    "20Cr-渗碳-淬火-回火"=>['min'=>560,'max'=>620, 'a'=>21],
    "20CrMnTi-渗碳-淬火-回火"=>['min'=>560,'max'=>620, 'a'=>22],
    "38CrMoAiA-调质-短期气体或液体氮化硬度<450"=>['min'=>300,'max'=>450, 'a'=>23],
    "38CrMoAiA-调质-短期气体或液体氮化硬度>450"=>['min'=>450,'max'=>850, 'a'=>24],
    "38CrMoAiA-调质-长期气体氮化硬度>450"=>['min'=>480,'max'=>550, 'a'=>25],
    "氮化钢(无铝钢)，HBS>500"=>['min'=>480,'max'=>550, 'a'=>26]
];
$arr = $_POST;

$zdl_cl = $arr['zdl_cl'];
$cdl_cl = $arr['cdl_cl'];
$arr['cl1'] = $cl1 = $HBS[$zdl_cl];
$arr['cl2'] = $cl2 = $HBS[$cdl_cl];

$year = $arr['year'];
$day = $arr['day'];
$hour = $arr['hour'];
if($year>30 || $year<5 || $day>365 || $day<180 || !$hour>24 || $hour<8){
    die('请选择正确的工作时间');
}

$lh = $year*$day*$hour;
$arr['lh'] = $lh;



$d = [
    '1'=>['min'=>0.8,'max'=>1.8],
    '3'=>['min'=>0.3,'max'=>0.8],
    '2'=>['min'=>0.6,'max'=>1.4],
];
$gear = $arr['gear'];

$dmin = $d[$gear]['min'];
$dmax = $d[$gear]['max'];


$u = $arr['u'];
$z1 = $arr['z1'];
if(!$u || !$z1){
    die('请选择传动比或齿数');
}
if ( $u<2 && $u>6 ) {
    die('传动比不合法');
}
$z2 = ceil($z1*$u);
$arr['z2'] = $z2;
$arr['zh'] = 2.5;

$Ka = [
    "pw" => [ "pw" => 1.00 , "qh" => 1.10 , "zz" => 1.25 , "qz" => 1.50 ],
    "qh" => [ "pw" => 1.25 , "qh" => 1.35 , "zz" => 1.50 , "qz" => 1.75 ],
    "zz" => [ "pw" => 1.50 , "qh" => 1.60 , "zz" => 1.75 , "qz" => 2.00 ],
    "qz" => [ "pw" => 1.75 , "qh" => 1.85 , "zz" => 2.00 , "qz" => 2.25 ],
];
$gzj = $arr['gzj'];
$ydj = $arr['ydj'];

$arr['ka'] = $Ka[$gzj][$ydj];
$ka = $Ka[$gzj][$ydj];
switch ($gzj){
    case 'pw':
        $arr['gzj'] = '平稳';
        break;
    case 'qh':
        $arr['gzj'] = '轻振';
        break;
    case 'zz':
        $arr['gzj'] = '中振';
        break;
    case 'qz':
        $arr['gzj'] = '强振';
        break;
}
switch ($ydj){
    case 'pw':
        $arr['ydj'] = '平稳';
        break;
    case 'qh':
        $arr['ydj'] = '轻振';
        break;
    case 'zz':
        $arr['ydj'] = '中振';
        break;
    case 'qz':
        $arr['ydj'] = '强振';
        break;
}

if(array_key_exists('dbl',$arr)){
    $j = 2;
    $j1 = '是';
}else{
    $j = 1;
    $j1 = '否';
}
if(array_key_exists('kaishi',$arr)){
    $k = '是';
}else{
    $k = '否';
}
$arr['j'] = $j1;
$arr['k'] = $k;

$n1 = $arr['n1'];
if(!$n1){
    die('请选择主动轮转速');
}
$arr['n'] = 60*$arr['n1']*$j*$lh;
$arr['n2'] = ceil($arr['n']/($z2/$arr['z1']));


function Z1($z1,$u){
    $z2 = $z1*$u;
    $aa1 = acos(($z1*cos(deg2rad(20)))/($z1+2));
    $aa2 = acos($z2*cos(deg2rad(20))/($z2+2));
    $ea = ($z1 * (tan($aa1) - tan( deg2rad(20) ) ) + $z2 * ( tan($aa2) - tan( deg2rad(20) ) ) ) / (2 * M_PI);
    return $ea;
}
$ea = Z1($arr['z1'],($z2/$arr['z1']));

$ze1 = round(sqrt((4-$ea)/3),3);
$arr['ze1'] = $ze1;
$arr['ea'] = $ea;


function Khn1($n){

    if($n>10000000000){
        die('工作时间或主动轮转速不合理');
    }else
        if($n<600000){
        return 1.6;
    }elseif($n>1000000000){
        return 0.95;
    }else{
        return 1.6*pow($n/500000,-6.931174E-02);
    }
}

function Khn2($n){
    if ( $n>=10000 && $n<100000 ) {
        return 1.6;
    } else if ($n>=1000000 && $n<50000000) {
        return 1.6*pow(($n/100000),-7.562885E-02);
    } else if ($n>=50000000 && $n<10000000000) {
        return 1;
    } else {
        die('工作时间或主动轮转速不合理');
    }
}


function Khn3 ($n) {

        if ( $n>=10000 && $n<100000 ) {
            return 1.3;
        } elseif ($n>=1000000 && $n<50000000) {
            return 1.3*pow($n/500000,-0.1892558);
        } elseif ($n>=50000000 && $n<10000000000) {
            return 1;
        }
        else {
            die('工作时间或主动轮转速不合理');
        }

}

function Khn4 ($n) {
    if ($n>=10000 && $n<100000) {
        return 1.1;
    } elseif($n>=1000000 && $n<50000000) {
        return 1.1*pow($n/500000,-6.875177E-02);
    } elseif ($n>=50000000 && $n<10000000000) {
        return 1;
    } else {
      die('工作时间或主动轮转速不合理');
    }
}
function Khn ($cl,$n) {
    if ($cl['a']<=4) {
        return Khn3($n);
    } elseif ($cl['a']<=8) {
        return Khn1($n);
    } elseif ($cl['a']>=23) {
        return Khn4($n);
    } else {
        return Khn2($n);
    }
}
$khn1 =Khn($cl1,$arr['n']);
$khn2 =Khn($cl2,$arr['n2']);
$arr['khn1'] = $khn1;
$arr['khn2'] = $khn2;


$t1 = 9550000*($arr['kw']/$arr['n1']);
$arr['t1'] = ceil($t1);
$forging1 = $arr['forging']?$arr['forging']:'';
$forging2 = $arr['forging1']?$arr['forging1']:'';
if($forging1 == 'dz'){
    $arr['forging'] = '锻造';
}else{
    $arr['forging'] = '铸造';
}
if($forging2=='dz'){
    $arr['forging1'] = '锻造';
}else{
    $arr['forging1'] = '铸造';
}

function ZE ( $cl1, $cl2, $forging1, $forging2 ) {
    if ( $cl1['a'] <= 2 ) {
        if ( $cl2['a'] <= 2 ){
            return 143.7;
        } else {
            die('从动轮材料选择选择不合法');
        }
    } elseif ( $cl1['a'] <= 4) {
        if ( $cl2['a'] <= 2 ) {
            return 156.6;
        } else if ( $cl2['a'] <= 4 ) {
            return 173.9;
        } else {
            die('从动轮材料选择不合法');
        }
    } elseif ( $forging1 == 'zz'){
        if ( $cl2['a'] <= 2){
            return 161.4;
        } else if ( $cl2['a'] <= 4){
            return 180.5;
        } else if ( $forging2 == 'zz') {
            return 188;
        } else {
            die('从动轮材料选择不合法');
        }
    } else {
        if ( $cl2['a'] <= 2){
            return 162;
        } else if ($cl2['a']<=4){
            return 181.4;
        } else if ($forging2 == 'zz'){
            return 188.9;
        } else {
            return 189.8;
        }
    }
}
$ze = ZE($cl1,$cl2,$forging1,$forging2);
$arr['ze'] = round($ze,3);

function HLIM($cl,$hbs){
    if($cl['a']<=2){
        return 1.1*$hbs+120;
    }elseif($cl['a']<=4){
        return 1.6*$hbs+170;
    }elseif ($cl['a'] == 7) {
        return $hbs+190;
    }elseif ($cl['a'] == 8) {
        return 0.9*$hbs+330;
    }elseif ($cl['a'] <= 11){
        return 0.8*$hbs+150;
    }else{
        return 1000;
    }
}
$hlim1 = HLIM($cl1,$arr['zdl_hbs']);
$hlim2 = HLIM($cl1,$arr['cdl_hbs']);


$s = $arr['s'];
$sa = $arr['sa'];
$kht = $arr['kht'];
if(!$s || !$sa || !$kht){
    die('请选择安全系数或载荷系数');
} elseif ($s>$sa) {
    die('接触安全系数应小于弯曲安全系数');
} elseif ($s<0.85 || $sa<1) {
    die('可靠度较低，请重新选择接触安全系数。');
} elseif ($s>1.6 || $sa >2) {
    die('安全系数不和法，请重新选择');
} elseif ($kht>3 || $kht<1){
    die('请重新选择合适的初选载荷系数');
}

$arr['alow_h1'] = $alow_h1 = round(($khn1*$hlim1)/$arr['s']);
$arr['alow_h2'] = $alow_h2 = round(($khn2*$hlim2)/$arr['s']);
$arr['alow_h'] = min(compact('alow_h1','alow_h2'));

$_SESSION['arr'] = json_encode($arr);

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        div{
            background-color:  #F0F0F0;
            margin: 30px auto;
            padding-top: 30px;
            width: 300px;
            text-align: center;
            height: 180px;
        }
    </style>
</head>
<body>
    <form action="3.php" method="post" id="form">
        <div>
            <p>选择齿宽系数</p>

            <p><input type="text" name="fd" id="fd"></p>
            <p>范围: <span id="min"><?=$dmin?></span>~<span id="max"><?=$dmax?></span></p>
            <input type="button" value="继续" id="btn">
            <input type="button" value="重新选择" onclick="location.href='index.php'">
            </tr>
        </div>
    </form>
</body>
</html>
<script>
   var btn = document.getElementById('btn');
   btn.onclick = function(){
       var fd= document.getElementById('fd').value;
       var min= document.getElementById('min').innerText;
       var max= document.getElementById('max').innerText;
       var form= document.getElementById('form');

       if(fd>=min && fd<=max){
           form.submit();
       }else{
           alert('您所输入的齿宽系数超出范围，请重新输入');
           return false;
       }
   }
</script>